package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.OrderRefundManager;
import com.fish.anne.model.OrderRefund;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class OrderRefundAction extends BaseAction implements Preparable {
    private OrderRefundManager orderRefundManager;
    private List orderRefunds;
    private OrderRefund orderRefund;
    private Long  refundId;

    public void setOrderRefundManager(OrderRefundManager orderRefundManager) {
        this.orderRefundManager = orderRefundManager;
    }

    public List getOrderRefunds() {
        return orderRefunds;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String orderRefundId = getRequest().getParameter("orderRefund.refundId");
            if (orderRefundId != null && !orderRefundId.equals("")) {
                orderRefund = orderRefundManager.get(new Long(orderRefundId));
            }
        }
    }

    public String list() {
        orderRefunds = orderRefundManager.getAll();
        return SUCCESS;
    }

    public void setRefundId(Long  refundId) {
        this. refundId =  refundId;
    }

    public OrderRefund getOrderRefund() {
        return orderRefund;
    }

    public void setOrderRefund(OrderRefund orderRefund) {
        this.orderRefund = orderRefund;
    }

    public String delete() {
        orderRefundManager.remove(orderRefund.getRefundId());
        saveMessage(getText("orderRefund.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (refundId != null) {
            orderRefund = orderRefundManager.get(refundId);
        } else {
            orderRefund = new OrderRefund();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (orderRefund.getRefundId() == null);

        orderRefundManager.save(orderRefund);

        String key = (isNew) ? "orderRefund.added" : "orderRefund.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}